package com.cms.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.model.TrAdminUser;
import com.cms.model.TrMenu;
import com.cms.service.MenuManagerService;
import com.cms.service.ReportService;
import com.google.gson.Gson;

@Controller
public class MenuManagerController extends BaseController {

	@Autowired
	private ReportService reportService;
	
	@Autowired
	private MenuManagerService menuManagerService;
	
    @RequestMapping("/activityManager.do")
    public String enterActivity(HttpServletRequest req,HttpServletResponse response,ModelMap map)throws Exception
    {
       TrMenu menu= new TrMenu();
       Map<String,Object>param = new HashMap<String, Object>();
       param.put(CmsConstant.CATALOG_NAME, "ACTIVITY_TYPE");
       map.put("ActivityType", this.reportService.getReportResult(CmsConstant.CATALOG_CODE, param));
       map.put("PactivityList", this.menuManagerService.queryMenu(menu));
	   return "activity/ActivityMain.html";
    }
    
    
    @RequestMapping("/listActivity.do")
    public String listActivity(HttpServletRequest req,HttpServletResponse response,ModelMap map)throws Exception
    {
       Map<String,Object>param = this.buildQueryMap(req);
       map.put("ActivityList",this.reportService.pagingReportResult("pagingActivityList", param));
	   return "activity/ActivityList.html";
    }
    
    @RequestMapping("/toAddActivity.do")
    public String toAddActivity(HttpServletRequest req,HttpServletResponse response,ModelMap map)throws Exception
    {
    	TrMenu menu= new TrMenu();
    	menu.setMenuType(CmsConstant.ACTIVITY_TYPE);
        Map<String,Object>param = new HashMap<String, Object>();
        param.put(CmsConstant.CATALOG_NAME, "ACTIVITY_TYPE");
        map.put("ActivityType", this.reportService.getReportResult(CmsConstant.CATALOG_CODE, param));
        map.put("PactivityList", this.menuManagerService.queryMenu(menu));
        map.put("activity", menu);
        map.put("action", "activityManager/saveActivity.do");
        return "activity/ActivityInfo.html";
    }
    
    
    @RequestMapping("/toEditActivity.do")
    public String toEditActivity(HttpServletRequest req,HttpServletResponse response,ModelMap map)throws Exception
    {
    	TrMenu menu= new TrMenu();
    	menu.setMenuType(CmsConstant.ACTIVITY_TYPE);
    	int activityId=StringUtils.str2Int(req.getParameter("activityId"));
    	menu.setId(activityId);
        Map<String,Object>param = new HashMap<String, Object>();
        param.put(CmsConstant.CATALOG_NAME, "ACTIVITY_TYPE");
        map.put("ActivityType", this.reportService.getReportResult(CmsConstant.CATALOG_CODE, param));
        map.put("PactivityList", this.menuManagerService.queryMenu(menu));
        map.put("activity", this.menuManagerService.getMenuInfo(menu));
        map.put("action", "activityManager/updateActivity.do");
        return "activity/ActivityInfo.html";
    }
    
    @RequestMapping("/activityManager/saveActivity.do")
    public void saveActivity(HttpServletRequest req,HttpServletResponse response,ModelMap map)throws Exception
    {
    	TrAdminUser tu= new TrAdminUser();
    	TrMenu menu = new TrMenu();
    	menu.setCreateUser(tu.getId());
    	menu.setModifyUser(tu.getId());
    	this.export(req, "activity", menu);
    	Map<String,Object>result = new HashMap<String, Object>();
    	try
    	{
    	     menuManagerService.saveMenu(menu);
    	     result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
    	}
    	catch (Exception e) {
    		 result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
    		 result.put(CmsConstant.MSG, e.getMessage());
		}
    	this.renderText(response, new Gson().toJson(result));
    }
    
    @RequestMapping("/activityManager/updateActivity.do")
    public void updateActivity(HttpServletRequest req,HttpServletResponse response,ModelMap map)throws Exception
    {
    	TrAdminUser tu= new TrAdminUser();
    	TrMenu menu = new TrMenu();
    	menu.setModifyUser(tu.getId());
    	this.export(req, "activity", menu);
    	Map<String,Object>result = new HashMap<String, Object>();
    	try
    	{
    	     menuManagerService.updateMenu(menu);
    	     result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
    	}
    	catch (Exception e) {
    		 result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
    		 result.put(CmsConstant.MSG, e.getMessage());
		}
    	this.renderText(response, new Gson().toJson(result));
    }
    
    
    @RequestMapping("/deleteActivity.do")
    public void deleteActivity(HttpServletRequest req,HttpServletResponse response,ModelMap map)throws Exception
    {
     	int activityId=StringUtils.str2Int(req.getParameter("activityId"));
     	TrMenu menu = new TrMenu();
     	menu.setId(activityId);
     	Map<String,Object>result = new HashMap<String, Object>();
    	try
    	{
    		 this.menuManagerService.deleteMenu(menu);
    	     result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
    	}
    	catch (Exception e) {
    		 result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
    		 result.put(CmsConstant.MSG, e.getMessage());
		}
    	this.renderText(response, new Gson().toJson(result));
     
    }
}
